package com.rzjm.api.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rzjm.api.entity.Parameter;
import com.rzjm.api.service.ParameterService;
import com.rzjm.api.service.Yun0533Service;
import com.rzjm.api.util.DateUtil;
import com.rzjm.api.util.OkHttpClientUtil;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @创建者：zhouwei
 * @创建时间：2022/1/12
 * @描述：
 */
@Service
public class Yun0533ServiceImpl implements Yun0533Service {

    @Resource
    private ParameterService parameterService;

    @Override
    public String getToken() {
        String token = "";
        long expiration = 0;
        Parameter parameter = parameterService.findById(1);
        if(parameter != null){
            if(parameter.getDExpireTime() == null || parameter.getDExpireTime().getTime() < new Date().getTime()){
                String loginName = parameter.getDLoginName();
                String password = parameter.getDPassword();
                String url = parameter.getDUrl();
                JSONObject jsonObject = null;
                OkHttpClientUtil instance = OkHttpClientUtil.getInstance();
                url = url + "api/getToken?loginName="+loginName + "&password=" + password;
                Response data = instance.getData(url);
                if (data != null && data.code() == 200) {
                    if(data.body()!=null) {
                        try {
                            jsonObject = JSONUtil.parseObj(data.body().string());
                            token = jsonObject.getJSONObject("data").getStr("token");
                            expiration = jsonObject.getJSONObject("data").getLong("expiration");
                            Date date = DateUtil.getTimeByTimspan(expiration);
                            parameter.setDExpireTime(date);
                            parameter.setDToken(token);
                            parameterService.updateById(parameter);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }else{
                token = parameter.getDToken();
            }
        }
        return token;
    }

    @Override
    public String getRealTimeData() {

        Parameter parameter = parameterService.findById(1);
        if(parameter != null){
            String token = getToken();

            String url = parameter.getDUrl();
            JSONObject jsonObject = null;
            OkHttpClientUtil instance = OkHttpClientUtil.getInstance();
            url = url + "api/data/getRealTimeData";
            Response data = null;

            Response response = instance.getData(url,token);
            try {
                String dataStr = response.body().string();
                System.out.println("dataStr: " + dataStr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("getRealTimeData 响应: ");
//            try {
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            if(StrUtil.isNotEmpty(data)){
//                jsonObject = JSONUtil.parseObj(data);
//
//            }
        }
        return null;
    }
}
